package com.sushi.service.impl;

import com.sushi.enums.Type;
import com.sushi.repository.entity.GameEntity;
import com.sushi.repository.mapper.GameMapper;
import com.sushi.service.GameService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sushi.response.ResponseBO;
import com.sushi.response.ResponsePageBO;
import com.sushi.response.GameResp;
import com.sushi.request.GameReq;
import com.sushi.request.GamePageReq;
import com.sushi.service.TagService;
import com.sushi.utils.*;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import lombok.extern.slf4j.Slf4j;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sushi.exception.BusinessException;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * 游戏 服务实现类
 * @date 2023-05-02
 * @time 17:14:28
 * @author Administrator
 */
@Slf4j
@Service
public class GameServiceImpl extends ServiceImpl<GameMapper, GameEntity> implements GameService {

	@Autowired
	private GameMapper gameMapper;

    @Autowired
    private TagService tagService;

    

    /**
     * 新增
     */
    @Override
    public ResponseBO<GameResp> add(GameReq req) {
        GameEntity entity = BeanUtil.convert(req, GameEntity.class);
        if (ObjectUtil.isEmpty(entity)) {
            throw new BusinessException("保存失败");
        }

        entity.insert();
        if (CollectionUtil.isNotEmpty(req.getTagIdList())) {
            tagService.saveRelation(req.getTagIdList(), entity.getId(), Type.Tag.GAME);
        }
        return ResponseUtil.ok(BeanUtil.convert(entity, GameResp.class));
    }

    /**
     * 逻辑删除
     */
    @Override
    public ResponseBO<GameResp> logicRemove(GameReq req) {
        if (StringUtil.isBlank(req.getId())) {
            throw new BusinessException("id不能为空");
        }
        GameEntity entity = new GameEntity();
        entity.setId(req.getId());
        entity.setDeleted(1L);
        entity.updateById();
        return ResponseUtil.ok(BeanUtil.convert(entity, GameResp.class));
    }

    /**
     * 更新
     */
    @Override
    public ResponseBO<GameResp> updateByField(GameReq req) {
        if (ObjectUtil.isEmpty(req) || StringUtil.isBlank(req.getId())) {
            throw new BusinessException("必要参数不能为空");
        }
        GameEntity entity = getById(req.getId());
        if (ObjectUtil.isEmpty(entity)) {
            return ResponseUtil.error("信息不存在，无法更新");
        }
        if (StringUtil.isNotBlank(req.getName())) {
            entity.setName(req.getName());
        }
        if (StringUtil.isNotBlank(req.getTags())) {
            entity.setTags(req.getTags());
        }
        if (ObjectUtil.isNotEmpty(req.getPrice())) {
            entity.setPrice(req.getPrice());
        }
        if (StringUtil.isNotBlank(req.getAuthor())) {
            entity.setAuthor(req.getAuthor());
        }
        if (StringUtil.isNotBlank(req.getPlatform())) {
            entity.setPlatform(req.getPlatform());
        }
        if (ObjectUtil.isNotEmpty(req.getSize())) {
            entity.setSize(req.getSize());
        }
        if (ObjectUtil.isNotEmpty(req.getSaleTime())) {
            entity.setSaleTime(req.getSaleTime());
        }
        if (StringUtil.isNotBlank(req.getStatus())) {
            entity.setStatus(req.getStatus());
        }

        if (CollectionUtil.isNotEmpty(req.getTagIdList())) {
            tagService.saveRelation(req.getTagIdList(), entity.getId(), Type.Tag.GAME);
        }

        entity.setGmtModifyTime(LocalDateTime.now());
        entity.setModifierId(UserUtil.getAdminId());
        entity.setModifierName(UserUtil.getAdminName());

        entity.updateById();
        log.info("更新成功：{}", JSON.toJSONString(entity));
        return ResponseUtil.ok(BeanUtil.convert(entity, GameResp.class));
    }

    /**
     * 列表
     */
    @Override
    public ResponsePageBO<GameResp> listByCondition(GamePageReq req) {
        if (ObjectUtil.isEmpty(req) || ObjectUtil.isEmpty(req.getPageNum()) || ObjectUtil.isEmpty(req.getPageSize())) {
            throw new BusinessException("必要参数不能为空");
        }
        LambdaQueryWrapper<GameEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(GameEntity::getDeleted, 0L);
        wrapper.eq(StringUtil.isNotBlank(req.getId()), GameEntity::getId, req.getId());
        wrapper.like(StringUtil.isNotBlank(req.getName()), GameEntity::getName, req.getName());
        wrapper.like(StringUtil.isNotBlank(req.getTags()), GameEntity::getTags, req.getTags());
//        wrapper.eq(ObjectUtil.isNotEmpty(req.getPrice()), GameEntity::getPrice, req.getPrice());
        wrapper.like(StringUtil.isNotBlank(req.getAuthor()), GameEntity::getAuthor, req.getAuthor());
        wrapper.like(StringUtil.isNotBlank(req.getPlatform()), GameEntity::getPlatform, req.getPlatform());
//        if (ObjectUtil.isNotEmpty(req.getPriceMin())) {
//            wrapper.ge(GameEntity::getPrice, req.getPriceMin());
//        }
//        if (ObjectUtil.isNotEmpty(req.getPriceMax())) {
//            wrapper.le(GameEntity::getPrice, req.getPriceMax());
//        }
//        if (ObjectUtil.isNotEmpty(req.getSizeMin())) {
//            wrapper.ge(GameEntity::getSize, req.getSizeMin());
//        }
//        if (ObjectUtil.isNotEmpty(req.getSizeMax())) {
//            wrapper.le(GameEntity::getSize, req.getSizeMax());
//        }
//        if (ObjectUtil.isNotEmpty(req.getSaleTimeStart())) {
//            wrapper.ge(GameEntity::getSaleTime, req.getSaleTimeStart());
//        }
//        if (ObjectUtil.isNotEmpty(req.getSaleTimeEnd())) {
//            wrapper.lt(GameEntity::getSaleTime, req.getSaleTimeEnd());
//        }
//        wrapper.eq(StringUtil.isNotBlank(req.getSize()), GameEntity::getSize, req.getSize());
//        wrapper.eq(ObjectUtil.isNotEmpty(req.getSaleTime()), GameEntity::getSaleTime, req.getSaleTime());
        wrapper.eq(StringUtil.isNotBlank(req.getStatus()), GameEntity::getStatus, req.getStatus());


        String searchContent = req.getSearchContent();
        if (StringUtil.isNotBlank(searchContent)) {
            wrapper.and(w -> w.or().like(GameEntity::getName, searchContent).or().like(GameEntity::getIntroduction, searchContent)
                    .or().like(GameEntity::getPlatform, searchContent));
        }

        wrapper.orderByDesc(GameEntity::getGmtCreateTime);

        Page<GameEntity> page = page(new Page<>(req.getPageNum(), req.getPageSize()), wrapper);
        if (ObjectUtil.isEmpty(page) || CollectionUtil.isEmpty(page.getRecords())) {
            return ResponsePageUtil.ok(req, new ArrayList<>(), 0L);
        }
        List<GameResp> convert = BeanUtil.convert(page.getRecords(), GameResp.class);
        return ResponsePageUtil.ok(req, convert, page.getTotal());
    }

    /**
     * 详情
     */
    @Override
    public ResponseBO<GameResp> detail(GameReq req) {
        if (ObjectUtil.isEmpty(req) || StringUtil.isBlank(req.getId())) {
            throw new BusinessException("必要参数不能为空");
        }
        GameEntity entity = getById(req.getId());
        if (ObjectUtil.isEmpty(entity)) {
            throw new BusinessException("对象不存在");
        }
        GameResp convert = BeanUtil.convert(entity, GameResp.class);
        tagService.getTagListByObjId(convert.getId(), Type.Tag.GAME);

        return ResponseUtil.ok(convert);
    }

}